class EnumBean {
  name: string;
  code: keyof any;
  cname?: string;
  desc?: string;
  constructor(name: string, code: keyof any, cname?: string, desc?: string) {
    this.name = name;
    this.code = code;
    this.cname = cname;
    this.desc = desc;
  }
  static init(name: string, code: keyof any, cname?: string, desc?: string) {
    return new EnumBean(name, code, cname, desc);
  }

  static get allBeans() {
    const beans: EnumBean[] = [];
    Object.entries(Object.getOwnPropertyDescriptors(this)).forEach(
      ([, value]) => {
        if (value.value instanceof EnumBean && value.writable) {
          beans.push(value.value);
        }
      }
    );
    return beans;
  }

  static getByCode(code: keyof any) {
    return this.allBeans.find((bean) => bean.code === code);
  }

  static getByName(name: string) {
    return this.allBeans.find((bean) => bean.name === name);
  }
}

export default EnumBean;
